package com.xh.core.controller;

import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.OpencvUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags="图像处理")
@RestController
@RequestMapping("/opencv")
public class OpencvController {

	@ApiOperation(value="图片黑白化处理")
    @PostMapping("/threshold")
    public AjaxResult threshold(String imgPath, HttpServletRequest request) throws Exception {
		String rootPath=request.getSession().getServletContext().getRealPath("/");
		String src = imgPath;
		String out = src;
    	if(src.contains("@_")){
    		out = src.substring(0, src.lastIndexOf("@_")+1)+
			(Integer.valueOf(src.substring(src.lastIndexOf("@_")+1,src.lastIndexOf(".")))+1)+src.substring(src.lastIndexOf("."));
    	}else{
    		out = src.substring(0, src.lastIndexOf("."))+"@_1"+src.substring(src.lastIndexOf("."));
    	}
    	OpencvUtils.imgThreshold(rootPath+src, rootPath+out);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", out);
    }
	
	@ApiOperation(value="去除图片中的某种颜色")
    @PostMapping("/removeColor")
	@ApiImplicitParams({
        @ApiImplicitParam(name="imgPath", value="图片路径", required = true, dataType = "String"),
	})
    public AjaxResult removeColor(String imgPath, String color, HttpServletRequest request) throws Exception {
		String rootPath=request.getSession().getServletContext().getRealPath("/");
		String src = rootPath + imgPath;
		String out = src;
    	if(src.contains("@_")){
    		out = src.substring(0, src.lastIndexOf("@_")+1)+
			(Integer.valueOf(src.substring(src.lastIndexOf("@_")+1,src.lastIndexOf(".")))+1)+src.substring(src.lastIndexOf("."));
    	}else{
    		out = src.substring(0, src.lastIndexOf("."))+"@_1"+src.substring(src.lastIndexOf("."));
    	}
    	OpencvUtils.removeAllColor(src, out);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", out);
    }
}
